(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var pageSize = 10;
	var currentPage = 1;
	var totalSize;
	var now = 0;
//	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTest(currentPage,pageSize);
		});
	});
	function initTest(currentpage,pageSize){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $(".students-choice-select",parent.document).attr("classcode");
		var starttime = strToDate($("#start-time").val() + "-01 00:00:00")/1000;
		var endtime = strToDate($("#end-time").val() + "-01 00:00:00")/1000;
		var obj = {
				currentpage:currentpage,
				schoolcode:schoolcode,
				classcode:classcode,
				pagesize:pageSize
		};
		if($("#start-time").val() != "" && $("#end-time").val() != ""){
			if(endtime <= starttime){
				$("#judge-time").text("结束时间要大于起始时间");
				$("#judge-time").show();
				return false;
			}
		}
		if($("#start-time").val() != "" && $("#end-time").val() != ""){
			obj.starttime = starttime;
			obj.endtime = endtime;
			$("#judge-time").hide();
		}else if($("#start-time").val() == "" && $("#end-time").val() == ""){
			$("#judge-time").hide();
		}else{
			$("#judge-time").text("请把时间填充完整");
			$("#judge-time").show();
			return false;
		}
		var data = {
				cmd:"getTeacherTestList",
				model:"bigexam",
				userKey:userKey,
				data:obj,
				remark:"",
		};
		processBegin("数据加载中······");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("查询成功");
				var data = res.data.data;
				if(data.length > 0){
					var total = res.data.total;
					totalSize = Math.ceil(total/pageSize);
					if(totalSize <= 5 && totalSize >0){
						for(var i = totalSize-1 ; i >= 0; i--){
							if(i == currentpage-1){
								pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
							}else{
								pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
							}
						}
					}else if(totalSize == 0){
						pagecontent += "<div class=\"pages-btn\">1</div>";
					}else if(totalSize > 5){
						var lastp = (now*5)+5;
						if(lastp > totalSize){
							lastp = totalSize;
						}
						for(var i = lastp-1 ; i >= (now*5); i--){
							if(i == currentpage-1){
								pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
							}else{
								pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
							}
						}
					}
					pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
					$("#student-page").empty();
					$("#student-page").append(pagecontent);
					
					$(".pages-btn").unbind("click");
					$(".pages-btn").click(function(){
						$(".pages-btn").removeClass("page-btn-select");
						$(this).addClass("page-btn-select");
						currentPage = parseInt($(this).text());
						initExam(currentPage,pageSize);
					});
					
					$(".pages-btn-last").unbind("click");
					$(".pages-btn-last").click(function(){
						if(totalSize <= 5 && totalSize >0){
							return false;
						}else if(totalSize == 0){
							return false;
						}else if(totalSize > 5){
							if(now > 0){
								now --;
								initExam((now*5+1),pageSize);
							}else{
								return false;
							}
						}
					});
					
					$(".pages-btn-next").unbind("click");
					$(".pages-btn-next").click(function(){
						if(totalSize <= 5 && totalSize >0){
							return false;
						}else if(totalSize == 0){
							return false;
						}else if(totalSize > 5){
							if(totalSize%5 == 0){
								if((now+1) < totalSize/5){
									now ++;
									initExam((now*5+1),pageSize);
								}else{
									return false;
								}
							}else{
								if((now+1) <= (totalSize-totalSize%5)/5){
									now ++;
									initExam((now*5+1),pageSize);
								}else{
									return false;
								}
							}
						}
					});
					if(data.length > 0){
						for(var i = 0; i < data.length; i++){
							for(var key in data[i]){
								if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
									data[i][key] = "";
								}
							}
							content += "<div class=\"table-trs\">" +
								"<div class=\"tables-left\" style='margin-left:15px;width:320px;'>" +
									"<div class=\"table-name\" style='width:320px;' subjectcode='"+data[i].subjectcode+"' examcode=\""+data[i].examcode+"\" examname=\""+data[i].examname+"\" classcode=\""+data[i].classcode+"\" schoolcode=\""+data[i].schoolcode+"\" title='"+data[i].examname+"'><a href='javascript:void(0)' class='into-exam-detail'>"+substr(data[i].examname,10) + "</a></div>" +
									"<div class=\"table-code\" style='width:320px;' title='"+data[i].describes+"'>"+substr(data[i].describes,6)+"</div>" +
								"</div>" +
								"<div class=\"tables-right\">" +
									"<div class=\"table-address\">创建于:"+getFormatDateByLong(parseInt(data[i].createtime)*1000,"yyyy-MM-dd hh:mm")+"</div>" +
									"<div class=\"table-oprate\">" +
										"<div class=\"table-score\"  style='margin-left:90px;' dataindex = \""+i+"\" examcode=\""+data[i].examcode+"\" examname=\""+data[i].examname+"\" classcode=\""+data[i].classcode+"\" schoolcode=\""+data[i].schoolcode+"\" title=\"成绩\"></div>" +
									"</div>" +
								"</div>" +
							"</div>";
						}
						$("#test-data-show").empty();
						$("#test-data-show").append(content);
						
						$(".table-trs").unbind("hover");
						$(".table-trs").hover(function(){
							$(this).find(".table-oprate").show();
						},function(){
							$(".table-oprate").hide();
						});
						
						$(".table-name").unbind("click");
						$(".table-name").click(function(){
							var examcode = $(this).attr("examcode");
							var examname = $(this).attr("examname");
							var classcode = $(this).attr("classcode");
							$("#detail-form-show").contents().find(".table-title").text(examname + "成绩单");
							$("#detail-form-show").show();
							$("#right-tables").hide();
							$(".title-search").hide();
							getTestScore(examcode,classcode);
							
							$("#detail-form-show").contents().find("#back-to-table").unbind("click");
							$("#detail-form-show").contents().find("#back-to-table").click(function(){
								$("#right-tables").show();
								$("#detail-form-show").hide();
								$(".title-search").show();
								$("#title-search-exam").show();
							});
						});
						//成绩
						$(".table-score").unbind("click");
						$(".table-score").click(function(){
							var examcode = $(this).attr("examcode");
							var examname = $(this).attr("examname");
							var classcode = $(this).attr("classcode");
							$("#detail-form-show").contents().find(".table-title").text(examname + "成绩单");
							$("#detail-form-show").show();
							$("#right-tables").hide();
							$(".title-search").hide();
							getTestScore(examcode,classcode);
							
							$("#detail-form-show").contents().find("#back-to-table").unbind("click");
							$("#detail-form-show").contents().find("#back-to-table").click(function(){
								$("#right-tables").show();
								$("#detail-form-show").hide();
								$(".title-search").show();
								$("#title-search-exam").show();
							});
						});
					}else{
						content += "<div class=\"table-trs\">" +
										"<div class=\"no-data-show\">没有查到相关结果！</div>" +
									"</div>";
						$("#test-data-show").empty();
						$("#test-data-show").append(content);
					}
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#test-data-show").empty();
					$("#test-data-show").append(content);
				}
			}else{
				processError("查询失败");
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#test-data-show").empty();
				$("#test-data-show").append(content);
			}
		},null,null);	
	}
	function getTestScore(examcode,classcode){
		var list = [];
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");	
		var studentcode = $(".students-choice-select",parent.document).attr("studentcode");
		var data = {
				cmd:"getTeacherResultList",
				model:"bigexam",
				userKey:userKey,
				data:{
					examcode:examcode,
					classcode:classcode,
//					schoolcode:schoolcode,
					studentcode:studentcode,
					type:1
				},
				remark:"",
				
		};
		processBegin("数据加载中······");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var data = res.data.data;
			if(res.result == 1 && res.data != undefined && res.data != ""){
				if(data.length > 0){
//					dataCache = [];
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
								"<div class=\"table-name\" style='width:120px;'  coursecontent='"+data[i].studentname+"' examcode=\""+data[i].examcode+"\" title='"+data[i].studentname+"'><a href='javascript:void(0)' class='studentname'>"+data[i].studentname + "</a></div>" +
								"<div class=\"table-code\" style='width:120px;'>"+data[i].studentcode+"</div>" +
							"</div>" +
							"<div class=\"tables-middle\" style='margin-left:25%;'>" +
								"<div class=\"table-class\" style='width:200px;'>分数： "+data[i].result+"</div>" +
								"<div class=\"table-code\" style='width:200px;'>"+data[i].grades+"年级"+data[i].classes+"班</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\">排名： 第"+data[i].classranking+"名</div>" +
							"</div>" +
						"</div>";
					}
					$("#detail-form-show").contents().find(".content-data-show").empty();
					$("#detail-form-show").contents().find(".content-data-show").append(content);
					processEnd("加载成功");
					
					$("#detail-form-show").contents().find("#leadingout-btn").unbind("click");
					$("#detail-form-show").contents().find("#leadingout-btn").click(function(){
						leadingOutTest();
					});
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#detail-form-show").contents().find(".content-data-show").empty();
					$("#detail-form-show").contents().find(".content-data-show").append(content);
					processEnd("加载成功");
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#detail-form-show").contents().find(".content-data-show").empty();
				$("#detail-form-show").contents().find(".content-data-show").append(content);
				processError("加载失败");
			}
		},null,null);
		return list;
	}
	//小测成绩导出
	function leadingOutTest(){
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var studentcode = $(".students-choice-select",parent.document).attr("studentcode");
		var classcode = $(".students-choice-select",parent.document).attr("classcode");
		var examcode = $("#score-form-show").contents().find(".table-name").attr("examcode");
		window.frames["export"].location = serverUrl.ExportUrl + "?t=" + Math.random() + "&cmd=getTeacherResultList" + "&examcode=" + examcode + "&classcode=" + classcode + "&schoolcode=" + schoolcode + "&fileName=" + new Date().getTime() + "&studentcode=" + studentcode;
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function pagebtns(){
		$("#exam-search-btn").click(function(){
			now = 0;
			initTest(1,pageSize);
		});
		$(".students-choice",parent.document).unbind("click");
		$(".students-choice",parent.document).click(function(){
			$(".students-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("students-choice-select");
			now = 0;
			initTest(1,pageSize);
			
		});	
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();